package com.crb.crm.manage.oos.aliyun;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.crb.crm.manage.enums.OssType;
import com.crb.crm.manage.oos.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @author huangjian
 */
@Slf4j
@Component
class AliyunOSSUtil implements OSSUtil {

    @Resource
    private AliyunOssConfig aliyunOssConfig;

    /**
     * @return OSSClient oss客户端
     * @throws
     * @Title: getOSSClient
     * @Description: 获取oss客户端
     */
    private OSSClient getOSSClient() {
        //使用你的对应的endpoint地址
        String endpoint = aliyunOssConfig.getEndpoint();
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录https://ram.console.aliyun.com 创建RAM账号。
        String accessKeyId = aliyunOssConfig.getAccessKey();
        String accessKeySecret = aliyunOssConfig.getSecretKey();
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        return ossClient;
    }

    @Override
    public OssType support() {
        return OssType.OSS_ALIYUN;
    }

    /**
     * @param inputStream 输入流
     * @param objectName  上传文件目录和（包括文件名） 例如“test/a.jpg”
     * @return void        返回类型
     * @throws
     * @Title: uploadByInputStream
     * @Description: 通过输入流上传文件
     */
    @Override
    public boolean upload(InputStream inputStream,
                          String objectName) {
        OSSClient ossClient = null;
        try {
            ossClient = getOSSClient();
            ossClient.putObject(aliyunOssConfig.getBucket(), objectName, inputStream);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return false;
    }


    /**
     * @param key 文件路径/名称，例如“test/a.txt”
     * @return void            返回类型
     * @throws
     * @Title: deleteFile
     * @Description: 根据key删除oss服务器上的文件
     */
    @Override
    public boolean delete(String key) {
        OSSClient ossClient = null;
        try {
            ossClient = getOSSClient();
            return ossClient.doesObjectExist(aliyunOssConfig.getBucket(), key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * @param key 文件路径和名称
     * @return InputStream    文件输入流
     * @throws
     * @Title: getInputStreamByOSS
     * @Description:根据key获取服务器上的文件的输入流
     */
    @Override
    public InputStream download(String key) {
        InputStream content = null;
        OSSClient ossClient;
        try {
            ossClient = getOSSClient();
            OSSObject ossObj = ossClient.getObject(aliyunOssConfig.getBucket(), key);
            content = ossObj.getObjectContent();
        } catch (Exception e) {
            e.printStackTrace();
        }/* finally {//这里不能关闭，否则文件无法下载下来
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }*/
        return content;
    }

}
